package org.test.reflect.ClassToMap;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;
import org.junit.Test;

public class ConverClassToMap {
	
	Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * 
	 * @param o 此对象的 field 必须都为string,
	 * @return
	 * @throws Exception 
	 * Map
	 * @author：denggm@asiainfo-linkage.com
	 * @Nov 3, 2012 3:38:54 PM
	 */
	public Map conver(Object o) throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		Class clazz = o.getClass();
		Field[] fieldArray = clazz.getDeclaredFields();
		for (Field onefield : fieldArray) {
			String fieldName = onefield.getName();
			onefield.setAccessible(true);
			String fieldVal = (String)onefield.get(o);
			map.put(fieldName, fieldVal);
		}
		return map;
	}
	
	@Test
	public void test1() throws Exception{
		TargetClass tc = new TargetClass();
		tc.setOwnId("1111");
		tc.setOwnName("Hello");
		
		Map<String, String> map = conver(tc);
		log.debug(map);
	}
}
